package com.xhwl.centiotdata.suppliersystem.haikang;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.HaiKangConstant;
import com.xhwl.centiotdata.pojo.dto.haikang.PersonInfoHaiKangDTO;
import com.xhwl.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/14/ 09:00
 */
@Service
public class UserHaiKangHandler {
    private static final Logger log = LoggerFactory.getLogger(UserHaiKangHandler.class);

    @Autowired
    private GeneralHaiKangHandler generalHaiKangHandler;

    // 添加人员信息
    public String addPerson(String personName, String gender, String orgIndexCode, List<String> faceDataList, Integer projectId, Integer productId, String ipAndPort, String ak, String sk) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("personName", personName);
        params.put("gender", gender);
        params.put("orgIndexCode", orgIndexCode);
        Map<String, Object> facesParams = new HashMap<>(faceDataList.size());
        faceDataList.stream().forEach(faceData -> {
            facesParams.put("faceData", faceData);
        });
        params.put("faces", facesParams);
        JSONObject dataObject = generalHaiKangHandler.post(HaiKangConstant.PERSON_ADD_URL, params, projectId, productId, ipAndPort, ak, sk, null);
        return dataObject.getString("personId");
    }

    public void deleteFace(String faceId, String personIds, Integer projectId, Integer productId, String ipAndPort, String ak, String sk) {
        if (StringUtils.isEmpty(faceId)) {
            JSONObject personListObject = this.listPerson(personIds, projectId, productId, ipAndPort, ak, sk);
            if (personListObject == null) {
                return;
            }
            JSONArray list = personListObject.getJSONArray("list");
            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            JSONArray personPhotoList = JSONObject.parseArray(list.get(0).toString());
            if (CollectionUtils.isEmpty(personPhotoList)) {
                return;
            }
            faceId = JSONObject.parseObject(personPhotoList.get(0).toString()).getString("personPhotoIndexCode");
        }
        Map<String, Object> params = new HashMap<>(1);
        params.put("faceId", faceId);
        generalHaiKangHandler.post(HaiKangConstant.FACE_DELETE_URL, params, projectId, productId, ipAndPort, ak, sk, null);
    }

    public JSONObject listPerson(String personIds, Integer projectId, Integer productId, String ipAndPort, String ak, String sk) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("pageNo", 500);
        params.put("pageSize", 1);
        params.put("personIds", personIds);
        JSONObject dataObject = generalHaiKangHandler.post(HaiKangConstant.PERSON_LIST_URL, params, projectId, productId, ipAndPort, ak, sk, null);
        return dataObject;
    }

    public List<PersonInfoHaiKangDTO> detailPerson(String paramName, List<String> paramValue, Integer projectId, Integer productId, String ipAndPort, String ak, String sk) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("paramName", paramName);
        params.put("paramValue", paramValue);
        JSONObject dataObject = generalHaiKangHandler.post(HaiKangConstant.PERSON_INFO_URL, params, projectId, productId, ipAndPort, ak, sk, null);
        JSONArray list = dataObject.getJSONArray("list");
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.toJavaList(PersonInfoHaiKangDTO.class);
    }

}
